package cn.edu.xust.service.impl;


import cn.edu.xust.Ro.PieData;
import cn.edu.xust.dao.SysUserDao;
import cn.edu.xust.entity.SysUserEntity;
import cn.edu.xust.service.HotelService;
import cn.edu.xust.service.SysUserService;
import cn.edu.xust.service.SysUserhotelService;
import cn.edu.xust.service.SysUserroleService;
import cn.edu.xust.util.PageUtils;
import cn.edu.xust.util.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {

    @Autowired
    HotelService hotelService;
    @Autowired
    SysUserroleService sysUserroleService;
    @Autowired
    SysUserhotelService sysUserhotelService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<SysUserEntity> wap = new QueryWrapper<>();

        for (Map.Entry<String, Object> item : params.entrySet()) {
            String key = item.getKey();
            Object value= item.getValue();
            if("gid".equals(key)){
                wap.eq(key,value);
            }
            if("makerid".equals(key)){
                wap.eq(key,value);
            }
            if("nickname".equals(key)&& !StringUtils.isEmpty(value.toString())){
                wap.like(key,value);
            }
            if("cname".equals(key)&& !StringUtils.isEmpty(value.toString())){
                wap.eq(key,value);
            }
        }


        IPage<SysUserEntity> page = this.page(
                new Query<SysUserEntity>().getPage(params),
                wap
        );

        return new PageUtils(page);
    }

    @Override
    public SysUserEntity getByNameAndPwd(String username, String pwd) {

        QueryWrapper<SysUserEntity> wap = new QueryWrapper<SysUserEntity>().eq("cname", username).eq("password", pwd);
        return baseMapper.selectOne(wap);


    }

    @Override
    public SysUserEntity getByName(String username) {
        QueryWrapper<SysUserEntity> wap = new QueryWrapper<SysUserEntity>().eq("cname", username);
        return baseMapper.selectOne(wap);


    }

    @Override
    public boolean updatePwd(Integer id, String newpwd) {
        UpdateWrapper<SysUserEntity> wap = new UpdateWrapper<SysUserEntity>().eq("uid", id);
        wap.set("password", newpwd);
        return baseMapper.update(null, wap) == 1;
    }
    @Override
    public boolean updatePwdNickName(Integer uid, String pwd, String nickname) {
        UpdateWrapper<SysUserEntity> wap = new UpdateWrapper<SysUserEntity>().eq("uid", uid);
        if(!StringUtils.isEmpty(pwd)){
            wap.set("password", pwd);
        }
        if(!StringUtils.isEmpty(nickname)){
            wap.set("nickname", nickname);
        }
        return baseMapper.update(null, wap) == 1;
    }

    @Override
    public SysUserEntity getByIdNameAndPwd(Integer id, String cname, String oldpwd) {
        QueryWrapper<SysUserEntity> wap = new QueryWrapper<SysUserEntity>().eq("cname", cname).eq("uid", id).eq("password", oldpwd);
        return baseMapper.selectOne(wap);
    }

    @Override
    public List<SysUserEntity> getdirectListByUser(SysUserEntity byNameAndPwd) {
        QueryWrapper<SysUserEntity> wap = new QueryWrapper<SysUserEntity>().eq("makerid", byNameAndPwd.getUid());
        return baseMapper.selectList(wap);
    }


    @Override
    public List<SysUserEntity> getListByUser(SysUserEntity byNameAndPwd) {
        return findSonNode(byNameAndPwd);
    }


    @Override
    public List<SysUserEntity> getByIds(List<Integer> uids) {
        QueryWrapper<SysUserEntity> wap = new QueryWrapper<SysUserEntity>().in("uid", uids);
        return baseMapper.selectList(wap);

    }

    @Override
    public boolean updateFbt(Integer uid, String config) {

        UpdateWrapper<SysUserEntity> wap = new UpdateWrapper<SysUserEntity>().eq("uid", uid);
        wap.set("config",config);
        return baseMapper.update(null,wap)==1;

    }

    @Override
    public Map<String, String> getgroupbymaikerid(List<Integer> makerids) {
        QueryWrapper<SysUserEntity> wap = new QueryWrapper<>();
        wap.in("makerid", makerids);
        wap.select("makerid as name","COUNT(1) as value");
        wap.groupBy("makerid");
        List<Map<String, Object>> maps =baseMapper.selectMaps(wap);

        return maps.stream()
                .collect(Collectors.toMap(m -> m.get("name")+"", m ->  m.get("value")+""));
    }


    /**
     * 这个结点的所有儿子所有儿子
     *
     * @param sysUser 当前需要遍历的结点
     * @return
     */
    private List<SysUserEntity> findSonNode(SysUserEntity sysUser) {

        //这个结点的所有儿子
        List<SysUserEntity> allUserList = new ArrayList<>();
        //找到直接子结点
        List<SysUserEntity> sysUserEntities = this.getdirectListByUser(sysUser);
        //加入所有
        allUserList.addAll(sysUserEntities);
        if (sysUserEntities.size() != 0) {
            //对每个直接儿子遍历找到他们的儿子
            for (SysUserEntity sysUserEntity : sysUserEntities) {
                List<SysUserEntity> sonNode = findSonNode(sysUserEntity);
                //找到以后放入全部
                allUserList.addAll(sonNode);
            }
        }
        return allUserList;
    }



}